<?php
class Admin_NguoiDungController extends Zend_Controller_Action
{

    public function init()
    {
         $this->_helper->layout->setLayout('admin');
    }

    public function indexAction()
    {
        $nguoidungMapper = Library_Model_NguoiDungMapper::getInstance();
        $nguoidung = $nguoidungMapper->nguoidungList();
        $this->view->nguoidung = $nguoidung;
        
        Zend_Registry::get('logger')->info($nguoidung);
    }
  public function listAction()
    {
    	$nguoidungMapper = Library_Model_NguoiDungMapper::getInstance();
    	$nguoidungs = $nguoidungMapper->getAllUsers();
    	$this->view->nguoidung = $nguoidungs;
    }

    public function themAction()
    {
    	$this->view->title = 'Thêm người dùng';
    	$formThem = new Admin_Form_ThemNguoiDungForm();
    	
    	$formThem->setMethod('post');
    	$formThem->setAction('/zfMHB/public/admin/nguoidung/them');
    	
    	$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
    	$this->view->form = $formThem;
    	
    	if($this->getRequest()->isPost()){
    		if($formThem->isValid($_POST)){
    			$data = $formThem->getValues();
    			
    			$tennd 						= $data['tennguoidung'];
    			$email 						= $data['email'];
    			$tendn 						= $data['tendangnhap'];
    			$matkhau				= md5($data['matkhau']);
    			$khoa 						= $data['khoa'];
    			$quyenhan 				= $data['quyenhan'];
    			$nguoidungMapper = Library_Model_NguoiDungMapper::getInstance();
    			
    			$nguoidungObj 	= new Library_Model_NguoiDung();    			
    			$nguoidungObj->setTennd($tennd);
    			$nguoidungObj->setEmail($email);
    			$nguoidungObj->setTendangnhap($tendn);
    			$nguoidungObj->setMatkhau($matkhau);
    			$nguoidungObj->setKhoa($khoa);
    			$nguoidungObj->setQuyenhan($quyenhan);
    			
    			$nguoidung  = $nguoidungMapper->save($nguoidungObj);
    			$this->_redirect('/admin/nguoidung/index');
    		}
    	}
    }

    public function capnhatAction()
    {
        $user_id = $this->getRequest()->getParam('id');
        $this->view->title = 'Cập nhật người dùng';
        $formCapnhat = new Admin_Form_ThemNguoiDungForm($user_id);
        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/nguoidung/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $nguoidungMapper = Library_Model_NguoiDungMapper::getInstance();
        $this->view->form = $formCapnhat;
        
        if($this->getRequest()->isPost()){
        	if($formCapnhat->isValid($_POST)){
        		$data = $formCapnhat->getValues();
        		
        		$id 					= $data['id'];
        		$tennd 				= $data['tennguoidung'];
        		$email 				= $data['email'];
        		$tendn 				= $data['tendangnhap'];
        		$matkhau 		= md5($data['matkhau']);
        		$khoa 				= $data['khoa'];
        		$quyenhan 		= $data['quyenhan'];
        		
        		$nguoidungObj = new Library_Model_NguoiDung();        		
        		$nguoidungObj->setId($id);
        		$nguoidungObj->setTennd($tennd);
        		$nguoidungObj->setEmail($email);
        		$nguoidungObj->setTendangnhap($tendn);
        		$nguoidungObj->setMatkhau($matkhau);
        		$nguoidungObj->setKhoa($khoa);
        		$nguoidungObj->setQuyenhan($quyenhan);
        		
        		$nguoidung = $nguoidungMapper->save($nguoidungObj);
        		
        		$this->_redirect('/admin/nguoidung/index/');
        	}
        } else {
        		$user = $nguoidungMapper->searchById($user_id);
        		
        		$formCapnhat->getElement('id')->setValue($user->getId());
        		$formCapnhat->getElement('tennguoidung')->setValue($user->getTennd());
        		$formCapnhat->getElement('email')->setValue($user->getEmail());
        		$formCapnhat->getElement('tendangnhap')->setValue($user->getTendangnhap());
        		$formCapnhat->getElement('matkhau')->setValue($user->getMatkhau());
        		$formCapnhat->getElement('khoa')->setValue($user->getKhoa());
        		$formCapnhat->getElement('quyenhan')->setValue($user->getQuyenhan());
        	}
        
    }

    public function xoaAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        $mand = $this->getRequest()->getParam('id');
        $nguoidungMapper = Library_Model_NguoiDungMapper::getInstance();
        $nguoidungMapper->delete($mand);
        $this->_redirect('/admin/nguoidung/index');
    }
    public function validateformAction()
    {
        //$user_id = $this->getRequest()->getParam('id');
        $this->_helper->viewRenderer()->setNoRender();
        $this->_helper->getHelper('layout')->disableLayout();
        $form = new Admin_Form_ThemNguoiDungForm();
        $form->isValid($this->_getAllParams());
        $json = $form->getMessages();
        header('Content-type: application/json');
        echo Zend_Json::encode($json);
    }


}









